﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FrameWork.Source.Animation_Classes;
using FrameWork.Source.Texture;
using FrameWork.Source.Resource_Manager;
using Boomer.Source.PathFinding;
using Microsoft.Xna.Framework;
using FrameWork.Source.Utilities;

namespace Boomer.Source.Character
{
    class CRageMonster : CMob
    {
        public enum MOBTYPE
        {
            MUMMYRAGE = 0,
            BOSSRAGE = 1
        }
        private bool isFixedPos;
        private MOBTYPE MobType;
        private CResourceManager.IMG_MAIN img;
        private float LocalTime;
        private int direction;

        public CRageMonster()
            : base()
        {
            this.Tag = FrameWork.Tag_Manager.TagManager.TAGGED.Enemy;
            isFixedPos = false;
            this.speed = 50.0f;
        }

        public void setMobType(MOBTYPE mobtype)
        {
            MobType = mobtype;
        }

        public override void init()
        {
            CAnimation[] pAnim;
            pAnim = new CAnimation[6];
            hp = 1;
            if (MobType == MOBTYPE.MUMMYRAGE)
            {
                pAnim[0] = new CAnimation();
                pAnim[0].createAnimation("Stand", 8, 8, 8, 1.0f);
                pAnim[1] = new CAnimation();
                pAnim[1].createAnimation("Stand Animation", 0, 8, 0, 1.0f / 10.0f);
                pAnim[2] = new CAnimation();
                pAnim[2].createAnimation("Move Left", 9, 14, 9, 1.0f / 10.0f);
                pAnim[3] = new CAnimation();
                pAnim[3].createAnimation("Move Right", 15, 20, 15, 1.0f / 10.0f);
                pAnim[4] = new CAnimation();
                pAnim[4].createAnimation("Move Top", 21, 26, 21, 1.0f / 10.0f);
                pAnim[5] = new CAnimation();
                pAnim[5].createAnimation("Move Bot", 27, 31, 27, 1.0f / 6.0f);

                img = CResourceManager.IMG_MAIN.MOBMUMMYRAGE;

                CTexture image = CResourceManager.getInstance().getImage((int)img);
                loadSprite(image, 32, 10, 56, 76, pAnim);
                createBoundBox(12.0f, 15.0f, 45, 30/*20.0f, 20.0f, 30, 25*/);
            }
            else
                if (MobType == MOBTYPE.BOSSRAGE)
                {
                    pAnim[0] = new CAnimation();
                    pAnim[0].createAnimation("Stand", 15, 15, 15, 1.0f);
                    pAnim[1] = new CAnimation();
                    pAnim[1].createAnimation("Stand Animation", 15, 15, 15, 1.0f / 4.0f);
                    pAnim[2] = new CAnimation();
                    pAnim[2].createAnimation("Move Left", 0, 4, 0, 1.0f / 6.0f);
                    pAnim[3] = new CAnimation();
                    pAnim[3].createAnimation("Move Right", 5, 9, 5, 1.0f / 6.0f);
                    pAnim[4] = new CAnimation();
                    pAnim[4].createAnimation("Move Top", 10, 14, 10, 1.0f / 6.0f);
                    pAnim[5] = new CAnimation();
                    pAnim[5].createAnimation("Move Bot", 15, 19, 15, 1.0f / 6.0f);

                    img = CResourceManager.IMG_MAIN.BOSSRAGE;

                    CTexture image = CResourceManager.getInstance().getImage((int)img);
                    loadSprite(image, 32, 10, 56, 67, pAnim);
                    createBoundBox(9.0f, 20.0f, 35, 25);
                }
            //base.init();
        }

        public override void update(float deltaTime)
        {
            if (!isDying)
            {
                CPathFinding pathFind = new CPathFinding(this.map);

                Vector2 mapPos = map.convertToMapPos(new Vector2(this.getBoundBox().X, this.getBoundBox().Y));
                Vector2 mapIndex = map.convertToMapIndex(mapPos);


                DIRECTION dir = DIRECTION.NONE;
                float speed = deltaTime * this.speed;
                if (pathFind.Dijkstra_Alg(mapIndex, map.playerPos) == 0)
                {
                    if (mapIndex.X < pathFind.posNext.X)
                    {
                        dir = DIRECTION.BOT;
                        this.changeAnimation("Move Bot");
                    }
                    if (mapIndex.X > pathFind.posNext.X)
                    {
                        dir = DIRECTION.TOP;
                        this.changeAnimation("Move Top");
                        speed = -speed;
                    }
                    if (mapIndex.Y < pathFind.posNext.Y)
                    {
                        dir = DIRECTION.RIGHT;
                        this.changeAnimation("Move Right");
                    }
                    if (mapIndex.Y > pathFind.posNext.Y)
                    {
                        dir = DIRECTION.LEFT;
                        this.changeAnimation("Move Left");
                        speed = -speed;
                    }
                    move(dir, speed);
                }
                else
                    if (pathFind.Dijkstra_Alg(mapIndex, new Vector2(9, 0)) == 1)
                    {
                        if (!isFixedPos)
                        {
                            Vector2 pos = new Vector2(this.getBoundBox().X, this.getBoundBox().Y);
                            if ((int)pos.X != (int)mapPos.X || (int)pos.Y != (int)mapPos.Y)
                            {
                                if ((int)pos.X > (int)mapPos.X + 5)
                                {
                                    dir = DIRECTION.LEFT;
                                    this.changeAnimation("Move Left");
                                    speed = -speed;
                                }
                                else
                                    if ((int)pos.X < (int)mapPos.X - 5)
                                    {
                                        dir = DIRECTION.RIGHT;
                                        this.changeAnimation("Move Right");
                                    }
                                    else
                                        if ((int)pos.Y > (int)mapPos.Y + 5)
                                        {
                                            dir = DIRECTION.TOP;
                                            this.changeAnimation("Move Top");
                                        }
                                        else
                                            if ((int)pos.Y < (int)mapPos.Y - 4)
                                            {
                                                dir = DIRECTION.BOT;
                                                this.changeAnimation("Move Bot");
                                                speed = -speed;
                                            }
                                move(dir, speed);
                            }
                            else
                            {
                                isFixedPos = true;
                            }
                        }
                        if (dir == DIRECTION.NONE)
                        {
                            this.changeAnimation("Stand");
                        }
                    }
                    else
                        if (pathFind.Dijkstra_Alg(mapIndex, map.playerPos) == -1)
                        {
                            LocalTime += deltaTime;
                            if (direction == 0)
                            {
                                this.changeAnimation("Stand");
                            }
                            if (isChangeDir)
                            {
                                isChangeDir = false;
                                direction = CUtilities.getInstance().randomNumber(0, 4);
                                LocalTime = 0;
                            }
                            if (LocalTime >= 5.0f)
                            {
                                direction = CUtilities.getInstance().randomNumber(0, 4);
                                LocalTime = 0;
                            }
                            else
                            {
                                if (direction == 3)
                                {
                                    dir = DIRECTION.BOT;
                                    this.changeAnimation("Move Bot");
                                }
                                else if (direction == 2)
                                {
                                    dir = DIRECTION.TOP;
                                    this.changeAnimation("Move Top");
                                    speed = -speed;
                                }
                                else if (direction == 1)
                                {
                                    dir = DIRECTION.RIGHT;
                                    this.changeAnimation("Move Right");
                                }
                                else if (direction == 0)
                                {
                                    dir = DIRECTION.LEFT;
                                    this.changeAnimation("Move Left");
                                    speed = -speed;
                                }
                                move(dir, speed);
                            }
                            if (dir == DIRECTION.NONE)
                            {
                                this.changeAnimation("Stand");
                            }  
                        }            
            }
            else
            {
                this.changeAnimation("Stand");
                dying(deltaTime);
            }
            base.update(deltaTime);
        }


        

        public override void draw()
        {
            base.draw();
        }

        public override bool Collision(ref global::FrameWork.Source.Object_Classes.CObject pObj)
        {
            return base.Collision(ref pObj);
        }

        public override void move(DIRECTION dir, float speed)
        {
            base.move(dir, speed);
        }
    }
}

